<template>
  <el-dialog
    :visible.sync="dialogVisible"
    :title="dailogName[dialogType]"
    append-to-body
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :show-close="false"
    :before-close="beforeClose"
    class="tag-edit-dialog factory-group-dialog"
    width="30%"
    v-dialogMove
  >
    <el-form :model="editForm" label-width="100px" :rules="editFormRules" ref="editForm">
      <el-form-item prop="groupNm" label="分组名称">
        <el-input v-model="editForm.groupNm" placeholder="分组名称"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="beforeClose">取消</el-button>
      <el-button type="primary" @click="submit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      editForm: {
        type: '',
        groupId: '',
        groupNm: '',
        groupType: 1
      },
      editFormRules: {
        groupNm: [
          { required: true, message: '分组名称不能为空', trigger: 'blur' },
          { validator: this.$checkEmpty('分组名称不能为空'), trigger: 'blur' }
        ]
      },
      dialogType: 'add',
      dailogName: {
        add: '新建分组',
        edit: '编辑分组'
      },
      dialogVisible: false
    }
  },
  methods: {
    showDialog(params) {
      this.dialogType = params.type
      this.editForm.groupType = params.groupType
      if (params.type === 'edit') {
        this.editForm.groupId = params.groupId
        this.editForm.groupNm = params.groupNm
      }
      this.dialogVisible = true
    },
    submit() {
      this.$refs.editForm.validate(valid => {
        if (valid) {
          let params = {
            groupNm: this.editForm.groupNm,
            groupType: this.editForm.groupType
          }
          if (this.dialogType === 'edit') {
            params.groupId = this.editForm.groupId
          }
          this.$ajax({
            url: this.$ajax.setUrl('ast/astTagDerivedGroup'),
            method: this.dialogType === 'edit' ? 'put' : 'post',
            data: this.$ajax.setData(params)
          }).then(({ data }) => {
            if (data && data.code === '0') {
              this.$message.success(data.msg)
              this.$emit('callback')
              this.beforeClose()
            } else {
              this.$message.error(data.data || data.msg)
            }
          })
        }
      })
    },
    beforeClose() {
      this.editForm.type = 'add'
      this.editForm.groupId = ''
      this.editForm.groupNm = ''
      this.editForm.groupType = 1
      this.$refs.editForm.clearValidate()
      this.dialogVisible = false
    }
  }
}
</script>

<style lang="scss"></style>
